package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EmergencyRescueFacilities;
import com.ruoyi.system.entity.SysRemind;
import com.ruoyi.system.mapper.EmergencyRescueFacilitiesMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.EmergencyRescueFacilitiesService;
import com.ruoyi.system.service.SysRemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 应急救援设施(EmergencyRescueFacilities)表服务实现类
 *
 * @author makejava
 * @since 2022-11-16 15:23:18
 */
@Service("emergencyRescueFacilitiesService")
public class EmergencyRescueFacilitiesServiceImpl implements EmergencyRescueFacilitiesService {
    @Autowired
    private SysRemindService sysRemindService;
    @Resource
    private EnterpriseWXServiceImpl enterpriseWXService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private EmergencyRescueFacilitiesMapper emergencyRescueFacilitiesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public EmergencyRescueFacilities queryById(Integer id) {
        return this.emergencyRescueFacilitiesDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param emergencyRescueFacilities 筛选条件
     * @param pageRequest               分页对象
     * @return 查询结果
     */
    @Override
    public Page<EmergencyRescueFacilities> queryByPage(EmergencyRescueFacilities emergencyRescueFacilities, PageRequest pageRequest) {
        long total = this.emergencyRescueFacilitiesDao.count(emergencyRescueFacilities);
        return new PageImpl<>(this.emergencyRescueFacilitiesDao.queryAllByLimit(emergencyRescueFacilities, pageRequest), pageRequest, total);
    }

    @Override
    public List<EmergencyRescueFacilities> selectList(EmergencyRescueFacilities emergencyRescueFacilities) {
        return emergencyRescueFacilitiesDao.queryAll(emergencyRescueFacilities);
    }

    /**
     * 新增数据
     *
     * @param emergencyRescueFacilities 实例对象
     * @return 实例对象
     */
    @Override
    public EmergencyRescueFacilities insert(EmergencyRescueFacilities emergencyRescueFacilities) {
        this.emergencyRescueFacilitiesDao.insert(emergencyRescueFacilities);
        return emergencyRescueFacilities;
    }

    /**
     * 修改数据
     *
     * @param emergencyRescueFacilities 实例对象
     * @return 实例对象
     */
    @Override
    public EmergencyRescueFacilities update(EmergencyRescueFacilities emergencyRescueFacilities) {
        this.emergencyRescueFacilitiesDao.update(emergencyRescueFacilities);
        return this.queryById(emergencyRescueFacilities.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.emergencyRescueFacilitiesDao.deleteById(id) > 0;
    }

    @Override
    public String importExcel(List<EmergencyRescueFacilities> emergencyRescueFacilitiess, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(emergencyRescueFacilitiess) || emergencyRescueFacilitiess.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        emergencyRescueFacilitiesDao.insertBatch(emergencyRescueFacilitiess);
        return "成功";
    }

    @Scheduled(cron = "0 0 8 * * ?")
    public void checkEmergencyRescueFacilities() {

        EmergencyRescueFacilities emergencyRescueFacilities = new EmergencyRescueFacilities();
        List<EmergencyRescueFacilities> emergencyRescueFacilitiesList = emergencyRescueFacilitiesDao.queryAll(emergencyRescueFacilities);
        if (CollectionUtils.isEmpty(emergencyRescueFacilitiesList)) {
            return;
        }

        for (EmergencyRescueFacilities var : emergencyRescueFacilitiesList) {
            String remind = var.getNextCheckTime();
            Date date = new Date();

            Date parseDate = DateUtils.parseDate(remind);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(parseDate);
            // 把日期往后增加一天,整数  往后推,负数往前移动
            calendar.add(Calendar.DATE, -1);

            boolean after = date.after(calendar.getTime());
            if (after) {
                String stringBuffer = "应急救援设施 设备名称:" + var.getSecurityEquipmentName() + "即将到期。";

               /* ViewConfigPushMes viewConfigPushMes = new ViewConfigPushMes();
                viewConfigPushMes.setViewName("安全许可证信息");
                List<ViewConfigPushMes> configPushMes = viewConfigPushMesMapper.queryByViewKey(viewConfigPushMes);
                if (!CollectionUtils.isEmpty(configPushMes)) {
                    ViewConfigPushMes pushMes = configPushMes.stream().findFirst().orElse(new ViewConfigPushMes());
                    if (com.ruoyi.common.utils.StringUtils.isNotEmpty(pushMes.getViewValue())) {
                        boolean contains = pushMes.getViewValue().contains(",");
                        if (contains) {
                            String[] split = pushMes.getViewValue().split(",");
                            for (String vars : split) {
                                sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(vars));
                            }
                        } else {
                            sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, Integer.valueOf(pushMes.getViewValue()));
                        }
                    }
                }*/

                SysRole sysRole = new SysRole();
                sysRole.setRoleKey("qyzzts");
                List<SysUser> sysUsers = sysUserMapper.selectUserByRoleName(sysRole);
                if (CollectionUtils.isEmpty(sysUsers)) {
                    return;
                }

                for (SysUser sys : sysUsers) {
                    sysRemindService.insert(SysRemind.TYPE_PROMPT, stringBuffer, sys.getUserId().intValue());
                }

                List<String> collect = sysUsers.stream().map(SysUser::getWxUserId).collect(Collectors.toList());
                EnterpriseWXServiceImpl.SendMessageRequest sendMessageRequest = new EnterpriseWXServiceImpl.SendMessageRequest();
                sendMessageRequest.setTouser(CollUtil.join(collect, "|"));
                sendMessageRequest.setMsgtype("text");
                sendMessageRequest.setAgentid(1000319);
                EnterpriseWXServiceImpl.MessageContent messageContent = new EnterpriseWXServiceImpl.MessageContent();
                messageContent.setContent(stringBuffer);
                sendMessageRequest.setText(messageContent);
                enterpriseWXService.pushMessage(sendMessageRequest);
            }
        }
    }


}
